package com.open.api.peekapi.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.open.api.peekapi.bean.ApiItem;
import com.open.api.peekapi.bean.ApiItemData;
import com.open.api.peekapi.dao.ApiItemDao;
import com.open.api.peekapi.services.ApiItemDataServices;
import com.open.api.peekapi.services.ApiItemServices;
import com.open.api.peekapi.util.HelpMe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class ExportController {

	private static Logger log = LoggerFactory.getLogger(ExportController.class);

	@Autowired
	ApiItemServices apiItemServices;
	@Autowired
	ApiItemDataServices apiItemDataServices;
	@Autowired
	ApiItemDao apiItemDao;



	@RequestMapping(value = "/export")
	public void download(HttpServletRequest request,HttpServletResponse response, String id) throws Exception{


		ApiItem one = apiItemDao.getOne(id);
		List<ApiItem> secondLevelList = apiItemServices.secondLevelByFirstLevelId(one.getId());


		String agent = request.getHeader("USER-AGENT").toLowerCase();

		response.reset();

		String fileName = one.getName()+"API文档";

		log.info("导出PDF文件 --> "+fileName+".pdf");

		fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
		if (agent.contains("firefox")) {
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".pdf");
		} else {
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".pdf");
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");



		//创建文件
		Document document = new Document();

		ServletOutputStream out = response.getOutputStream();

		//建立一个书写器
		PdfWriter writer = PdfWriter.getInstance(document, out);

		//打开文件
		document.open();

		fillData(document,secondLevelList);

		//关闭文档
		document.close();
		//关闭书写器
		writer.close();

	}






	private void fillData(Document document,List<ApiItem> dataList)throws Exception{

		BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font font = new Font(bfChinese);

		String line = System.lineSeparator();

		for (ApiItem data:dataList){
			List<ApiItemData> tempList = apiItemDataServices.data(data.getId());

			//创建章节
			Paragraph chapterTitle = new Paragraph(data.getName(), font);
			Chapter chapter = new Chapter(chapterTitle, 1);
			chapter.setNumberDepth(0);


			for (ApiItemData temp:tempList){
				Paragraph sectionTitle = new Paragraph(temp.getName(), font);
				Section section = chapter.addSection(sectionTitle);
				StringBuffer sb = new StringBuffer();
				sb.append(line);
				sb.append("请求方式: "+temp.getRequestType());sb.append(line+line);
				sb.append("请求URL: http://{ip}:{port}/{projectName} "+temp.getRequestUrl());sb.append(line+line);
				sb.append("请求参数: "+line+temp.getRequest());sb.append(line+line);
				sb.append("返回参数: "+line+temp.getResponse());sb.append(line+line);
				if (HelpMe.isNotNull(temp.getDescr())){
					sb.append("接口其它说明: "+line+temp.getDescr());sb.append(line);
				}
				sb.append(line+line);

				Paragraph sectionContent = new Paragraph(sb.toString(),font);
				section.add(sectionContent);
			}

			//将章节添加到文章中
			document.add(chapter);
		}
	}


}



















